//int __stdcall AlphaBlend_8u_C4A(unsigned char* pSrc, int srcStep, unsigned char* pAlp, int AlpStep, unsigned char* pDst, int dstStep, IppiSize roiSize, int scaleFactor);


#include <asm.h>

.global C_SYMBOL(AlphaBlend_8u_C4A)

C_SYMBOL(AlphaBlend_8u_C4A):


    STMFD	SP!, {R4-R12,LR}
Start:
    MOV	LR, R0
//.long 0xe7fddefe
    LDR R11, [LR, #0x0]                 //pSrc
    LDR	R8,  [LR, #0x8]                 //SrcStep
    LDR	R10, [LR, #0x4]                //pDst
	LDR	R12, [LR, #0xC]                //DstStep

    LDR	R4, [LR, #0x10]                 //width
	LDR	R7, [LR, #0x14]                 //height
	LDRB	R3, [LR, #0x1C]
    ORR R3, R3, R3, LSL #16
    TMCRR   wR14, R3, R3

	MOV LR, R4, LSL #2

    LDR     R4, = 0x00FF00FF
    TMCRR   wR15, R4, R4

    LDR     R4, = 0x00000008
    TMCR    wCGR3, R4


LoopLine:
	MOV	R3, LR
	MOV	R2, R10
	MOV	R0, R11
	B Alpha_8u_C4ARow
Next:
	SUB	R7, R7,	#1
	ADD	R11, R11, R8
	ADD	R10, R10, R12
	CMP	R7, #0
	BHI	LoopLine

End:

	LDMFD	SP!, {R4-R12,PC}



Alpha_8u_C4ARow:


    ADD	R6, R2,	#7
    BIC	R6, R6,	#7
    SUB	R6, R6,	R2
    CMP	R6, #0
    BEQ	_Aligned8_

_Process_One_:

    WLDRW   wR4, [R0], #4
    CMP	R6, R3
    WLDRW   wR6, [R2]

    MOVGT	R6, R3
    SUB	R3, R3,	R6

_Loop_One_:
//.long 0xe7fddefe
    SUBS	R6, R6,	#4
    WMOV    wR5,wR14
    WUNPCKELUB wR0, wR4
    WUNPCKELUB wR1, wR6
    WMULUL  wR0, wR0, wR5
    WSUBH   wR7,wR15,wR5
    WMULUL  wR1, wR1, wR7

    WLDRWGT   wR4, [R0], #4
    WADDH     wR0, wR0, wR1
    WLDRWGT   wR6, [R2, #4]
    WSRLHG    wR0, wR0, wCGR3

    WPACKHUS wR0, wR0, wR1

    //ADD R2,R2,#4
    WSTRW   wR0, [R2], #4

    BGT	_Loop_One_

    CMP	R3, #0
    BEQ     Next

_Aligned8_:
    SUBS	R3, R3,	#0x10
    ADDMI	R6, R3,	#0x10
    ADDMI	R3, R3,	#0x10
    BMI	_Process_One_
    AND	R4, R0,	#7
    CMP R4, #0
    BNE	_UnAligned8_

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_Aligned8_:

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    WUNPCKELUB wR6, wR0

    WMOV    wR7, wR14
    WUNPCKELUB wR8, wR4

    WMULUL  wR6, wR6, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7


    WUNPCKEHUB wR9, wR0

    WMOV    wR10,wR14

    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3

    WMULUL  wR9, wR9, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WLDRDGE wR0, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9
    WSTRD   wR6, [R2], #8


    WUNPCKELUB wR6, wR2

    WMOV    wR7,wR14
    WUNPCKELUB wR8, wR5
    WMULUL  wR6, wR6, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    WUNPCKEHUB wR9, wR2

    WMOV    wR10,wR14
    WUNPCKEHUB wR11, wR5

    WMULUL  wR9, wR9, wR10

    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10
    WLDRDGE wR2, [R0], #8
    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3
    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]
    WSTRD wR6, [R2], #8


    BGE	_Loop_Aligned8_
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    //SUBGT   R2, R2, #8
    BGT	_Process_One_

_UnAligned8_:
    BIC	R0, R0,	#7
    TMCR    wCGR1, R4

    WLDRD   wR0, [R0], #8
    WLDRD   wR4, [R2]

    WLDRD   wR2, [R0], #8
    WLDRD   wR5, [R2, #8]

_Loop_UnAligned8_:
@.long 0xe7fddefe
    TST	R4, #7
    WLDRDNE wR12, [R0]

    SUBS	R3, R3,	#0x10
    PLD	[R0,#0x20]
    PLD	[R2,#0x30]

    WALIGNR1 wR0 ,wR0, wR2


    WUNPCKELUB wR6, wR0

    WMOV    wR7,wR14
    WUNPCKELUB wR8, wR4
    WMULUL  wR6, wR6, wR7
    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7


    WUNPCKEHUB wR9, wR0

    WMOV    wR10,wR14
    WADDH     wR6, wR6, wR8
    WUNPCKEHUB wR11, wR4
    WSRLHG  wR6, wR6, wCGR3


    WMULUL  wR9, wR9, wR10
    WSUBH   wR10,wR15,wR10
    WMULUL  wR11, wR11, wR10

    WLDRDGE wR0, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3

    WPACKHUS wR6, wR6, wR9

    //ADD R2,R2,#8

    WSTRD   wR6, [R2], #8


    WALIGNR1 wR2 ,wR2, wR12


    WUNPCKELUB wR6, wR2

    WMOV    wR7,wR14
    WUNPCKELUB wR8, wR5

    WMULUL  wR6, wR6, wR7

    WSUBH   wR7,wR15,wR7
    WMULUL  wR8, wR8, wR7

    WLDRDGE wR4, [R2, #8]

    WADDH     wR6, wR6, wR8
    WSRLHG    wR6, wR6, wCGR3


    WUNPCKEHUB wR9, wR2

    WMOV    wR10,wR14
    WUNPCKEHUB wR11, wR5

    WMULUL  wR9, wR9, wR10

    WSUBH   wR10, wR15, wR10
    WMULUL  wR11, wR11, wR10

    WLDRDGE wR2, [R0], #8

    WADDH   wR9, wR9, wR11
    WSRLHG  wR9, wR9, wCGR3


    WPACKHUS wR6, wR6, wR9
    WLDRDGE wR5, [R2, #16]

    //ADD R2,R2,#8
    WSTRD   wR6, [R2], #8

    BGE	_Loop_UnAligned8_

    ADD	R0, R0,	R4
    ADDS	R3, R3,	#0x10
    BEQ     Next
    MOVGT	R6, R3
    BGT	_Process_One_
